// pages/teacherAffairs/teacherAffairs.js
var search = require('../boss/search');
const App = getApp()
var util = require('../../utils/util');

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

const teacherReduceType = {
  0: '正常',
  1: '迟到',
  2: '步骤未完成',
  3: '位置异常',
  4: 'vip签到异常',
}

Page({
  data: {
    role: '', //角色
    tabList: [{
        name: '待续费',
        value: 1
      },
      {
        name: '乐团异常',
        value: 2
      },
      {
        name: 'vip异常',
        value: 3
      },
      {
        name: '课表',
        value: 4
      },
      {
        name: '学生查询',
        value: 5
      },
    ],
    currentItem: 1,
    search_list: search.searchList,
    status_list: search.statusList,
    statusObj: search.statusObj,
    payInfoData: {}, //回款详情
    consumption: {}, // 课时消耗
    vipTeacherList: [], //vip老师列表
    bigTeacherList: [], //团课老师列表
    currentTime: "", //当前课时
    currentVip: "", //vip老师
    currentStatus: "", //当前状态
    currentBig: "", //团课老师
    pendingList: [], //待续费列表
    abnormalBigTimetableList: [], // 团课异常列表
    abnormalVipTimetableList: [], // vip异常列表
    studentList: {}, // 学生查询结果
    keyWord: "", //学生查询条件
    date: util.formatTime(new Date()), //课表日期
    courseList: {}, //课表数据
    scheduleData: {
      type: '', // 调课类型
      date: util.formatTime(new Date()), //调课时间
      status: false, //调课弹窗
      id: '', //需要调课的课时id
      time: '00:00'
    }, //调课信息
    trackingData: { // 跟进记录
      id: "",
      content: "",
      status: false
    },
    bossCheckPage: [],
    teacherReduceType: teacherReduceType,
    vipCurriculumList: [{
      name: '团课',
      id: false
    }, {
      name: 'vip课',
      id: true
    }],
    currentvipCurriculum: false
  },
  onLoad(options) {
    let role = App.globalData.userInfo?.roles[0]
    this.setData({
      role: role
    })
    this.getPayInfo()
    // this.getconsumption()
    this.getVipTeacherList()
    this.getBigTeacherList()
    this.getListData(this.data.currentItem)
  },
  onShow() {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0
      })
    }
  },
  jumpBackMony() {
    // 跳转回款详情
    wx.navigateTo({
      url: `/pages/teacherLeadBackMoney/teacherLeadBackMoney`,
    })
  },
  jumpTimeConsumption() {
    // 跳转课时消耗
    wx.navigateTo({
      url: `/pages/timeConsumption/timeConsumption`,
    })
  },
  async getPayInfo() {
    // 获取回款信息
    let data
    const year = new Date().getFullYear()
    const month = new Date().getMonth() + 1
    let date = `${[year, month].map(formatNumber).join('-')}`
    data = await App.$axios.request({
      url: "/educational/overview/paymentReceipttProgress",
      method: 'GET',
      loading: true,
      data: {
        queryMonth: date
      }
    })
    if (data) {
      this.setData({
        payInfoData: data
      })
    }
  },
  changeTab(item) {
    // 状态栏切换
    console.log(item.currentTarget.dataset.iteminfo)
    let itemInfo = item.currentTarget.dataset.iteminfo
    if (this.data.currentItem == itemInfo.value) return
    this.setData({
      currentItem: itemInfo.value,
      currentTime: "", //当前课时
      currentVip: "", //vip老师
      currentStatus: "", //当前状态
      currentBig: "", //团课老师
      pendingList: [], //待续费列表
      abnormalBigTimetableList: [], // 团课异常列表
      abnormalVipTimetableList: [], // vip异常列表
      keyWord: "", //学生查询条件
      courseList: {}, //课表数据
    })
    this.getListData(itemInfo.value)
  },
  async getVipTeacherList() {
    // vip老师获取
    let data = await App.$axios.request({
      url: "/educational/curriculum/abnormalVipTeacherList",
      method: 'GET',
      loading: true,
    })
    if (data) {
      this.setData({
        vipTeacherList: data
      })
    }
  },
  async getBigTeacherList() {
    // 团课老师获取
    let data = await App.$axios.request({
      url: "/educational/curriculum/abnormalBigTeacherList",
      method: 'GET',
      loading: true,
    })
    if (data) {
      this.setData({
        bigTeacherList: data
      })
    }
  },
  async getListData(type) {
    // 根据type请求接口
    switch (type) {
      case 1:
        this.pendingRenewal()
        break;
      case 2:
        this.abnormalBigTimetable()
        break;
      case 3:
        this.abnormalVipTimetable()
        break;
      case 4:
        this.getTimetableDetailQuery()
        break;
      case 5:

        break;
      default:
        break;
    }
  },
  async pendingRenewal() {
    // 获取待续费列表
    let data = await App.$axios.request({
      url: "/educational/curriculum/pendingRenewal",
      method: 'GET',
      loading: true,
      data: {
        teacherUserId: this.data.currentVip,
        count: this.data.currentTime
      }
    })
    if (data) {
      this.setData({
        pendingList: data
      })
    }
  },
  async abnormalBigTimetable() {
    // 获取乐团异常列表
    let data = await App.$axios.request({
      url: "/educational/curriculum/abnormalBigTimetable",
      method: 'GET',
      loading: true,
      data: {
        teacherUserId: this.data.currentBig,
        count: this.data.currentTime,
        teacherReduceType: this.data.currentStatus
      }
    })
    if (data) {
      this.setData({
        abnormalBigTimetableList: data
      })
    }
  },
  async abnormalVipTimetable() {
    // 获取vip异常列表
    let data = await App.$axios.request({
      url: "/educational/curriculum/abnormalVipTimetable",
      method: 'GET',
      loading: true,
      data: {
        teacherUserId: this.data.currentVip,
        count: this.data.currentTime
      }
    })
    if (data) {
      this.setData({
        abnormalVipTimetableList: data
      })
    }
  },
  changeSearch(e) {
    let word = e.currentTarget.dataset
    if (word.type) {
      let typeData = word.iteminfo.id
      if (this.data[word.type] == word.iteminfo.id) typeData = ""
      this.setData({
        [word.type]: typeData
      })
      this.getListData(this.data.currentItem)
    } else {
      // 学生查询
      this.setData({
        keyWord: e.detail.value
      })
    }
  },
  changeVipSearch(e) {
    let word = e.currentTarget.dataset.iteminfo.id
    if (this.data.currentvipCurriculum === word) return
    this.setData({
      currentvipCurriculum: word
    })
    this.getListData(this.data.currentItem)
  },
  async getTimetableDetailQuery() {
    // 课表列表
    let data = await App.$axios.request({
      url: "/educational/curriculum/timetableDetailList",
      method: 'GET',
      loading: true,
      data: {
        queryDate: this.data.date,
        vipCurriculum: this.data.currentvipCurriculum
      }
    })
    if (data) {
      this.setData({
        courseList: data
      })
    }
  },
  async studentQuery(e) {
    // 学生查询
    if (!this.data.keyWord) {
      wx.showToast({
        title: '请输入搜索内容',
        icon: "error"
      })
      return
    }
    let data = await App.$axios.request({
      url: "/educational/curriculum/studentQuery",
      method: 'GET',
      loading: true,
      data: {
        keyWord: this.data.keyWord
      }
    })
    if (data) {
      this.setData({
        studentList: data
      })
    }
  },
  bindDateChange(date) {
    this.setData({
      date: date.detail.value,
    })
    this.getTimetableDetailQuery()
  },
  jumpDetail(item) {
    // 调课
    let itemData = item.currentTarget.dataset.iteminfo
    this.setData({
      'scheduleData.status': true,
      'scheduleData.id': itemData.id,
      'scheduleData.time': itemData.startTime,
      'scheduleData.date': itemData.yearMonthDay
    })
  },
  changeSchedule(e) {
    // 调课类型选择
    this.setData({
      'scheduleData.type': e.detail.value
    })
  },
  changeScheduleDate(date) {
    // 选期调课选择时间
    this.setData({
      'scheduleData.date': date.detail.value,
    })
  },
  changeScheduleTime(date) {
    // 选期调课选择时间
    this.setData({
      'scheduleData.time': date.detail.value,
    })
  },
  async scheduleBoxClick(e) {
    //  调课弹窗点击
    if (e.currentTarget.dataset.type == 'ok' && !this.data.scheduleData.type) {
      wx.showToast({
        title: '请先选择调课类型',
        icon: "none"
      })
    } else if (e.currentTarget.dataset.type == 'cancel') {
      this.setData({
        scheduleData: {
          type: '', // 调课类型
          date: util.formatTime(new Date()), //调课时间
          status: false, //调课弹窗
          id: '', //需要调课的课时id
          time: '00:00',
        }
      })
    } else {
      let url //调课请求的url
      let query = {
        // 请求参数
        id: this.data.scheduleData.id
      }
      if (this.data.scheduleData.type == 1) {
        // 顺延调课
        url = '/educational/curriculum/scheduleDelay'
      } else if (this.data.scheduleData.type == 2) {
        if (!this.data.scheduleData.date) {
          wx.showToast({
            title: '调课日期不可为空',
            icon: "none"
          })
          return
        }
        // 选日期调课
        query.yearMonthDay = this.data.scheduleData.date
        query.startDateTime = `${this.data.scheduleData.date} ${this.data.scheduleData.time}`
        url = '/educational/curriculum/scheduleUpdate'
      }

      let data = await App.$axios.request({
        url: url,
        method: 'PUT',
        loading: true,
        data: query
      })
      if (data === true) {
        this.setData({
          scheduleData: {
            type: '',
            date: util.formatTime(new Date()),
            status: false,
            id: '',
            time: '00:00',
          },
        })
        if (this.data.currentItem == 1) {
          this.getRecentlyTimetable()
        } else if (this.data.currentItem == 4) {
          this.studentQuery()
        }
      }
    }
  },
  openAddTracking(e) {
    // 新增跟进框打开
    console.log(e.currentTarget.dataset.item)
    let item = e.currentTarget.dataset.item
    this.setData({
      'trackingData.id': item.vipClassId,
      'trackingData.status': true
    })
  },
  trackingChange(e) {
    // 跟进记录内容修改
    this.setData({
      'trackingData.content': e.detail.value
    })
  },
  async trackingClick(e) {
    // 新增跟进
    if (e.currentTarget.dataset.type == 'ok') {
      if (!this.data.trackingData.content) {
        wx.showToast({
          title: '跟进内容不可为空',
          icon: 'none'
        })
        return
      }
      let data = await App.$axios.request({
        url: '/educational/curriculum/tracking',
        method: 'PUT',
        loading: true,
        data: {
          vipClassId: this.data.trackingData.id,
          trackingContent: this.data.trackingData.content
        }
      })
      if (data == true) {
        this.setData({
          trackingData: { // 跟进记录
            id: "",
            content: "",
            status: false
          }
        })
      }
      this.pendingRenewal()
    } else {
      this.setData({
        'trackingData.status': false
      })
    }
  },
  tellPhone(info) {
    console.log(info.currentTarget.dataset.phone)
    let phone = info.currentTarget.dataset.phone
    if (!phone) return
    wx.makePhoneCall({
      phoneNumber: phone
    })
  },
  changeTime(e) {
    let item = e.currentTarget.dataset.iteminfo
    console.log(item)
    this.setData({
      'scheduleData.status': true,
      'scheduleData.id': item.id,
      'scheduleData.type': e.currentTarget.dataset.type,
      'scheduleData.time': item.startTime,
      'scheduleData.date': item.yearMonthDay
    })
  },
  async changeStatus(e) {
    let item = e.currentTarget.dataset.iteminfo
    let url
    if (e.currentTarget.dataset.type == 'true') {
      url = "/educational/curriculum/leave"
    } else {
      url = "/educational/curriculum/leaveCancel"
    }
    let data = await App.$axios.request({
      url: url,
      method: 'PUT',
      loading: true,
      data: {
        timetableDetailedId: item.id,
        studentUserId: e.currentTarget.dataset.studentid
      }
    })
    if (data) {
      wx.showToast({
        title: '操作成功',
      })
      this.studentQuery()
    }
  }
})